﻿$(document).ready(function () {
    $.ajax({
        url: 'Data.txt',
        type: "GET",
        dataType: "JSON",
        async: true,
        contentType: "charset=utf-8",
        success: function (data) {
            if (!data)
                alert("No data returned.");
            else {
                alert($("#Products").val());
                for (var i = 0; i < data.length; i++) {
                    var bestSell = "";
                    var productsRecommendItems = "";
                    var productsNewItems = "";

                    bestSell += "<div class='productItems'>";
                    productsRecommendItems += "<div class='productItems RecommendItems'>";
                    if (data[i].Sale != 0) {
                        bestSell += "<div class='productSale'>-" + data[i].Sale + "%</div>";
                        productsRecommendItems += "<div class='productSale'>-" + data[i].Sale + "%</div>";
                    }

                    bestSell += "<a class='productimg' href='#'>" + "<img alt='' src='" + data[i].Image + "' />";
                    //productsRecommendItems
                    if (data[i].Tep != 0) {
                        bestSell += "<span class='producteps'>Tập 64/120</span> </a>";
                    } else {
                        bestSell += "</a>";
                    }

                    bestSell += "<a class='productlink' href='#'>" + data[i].Title + "</a>";
                    bestSell += "<div class='productAuthor'>Tác giả: <a href='#'>" + data[i].Author + "</a></div>";
                    bestSell += "<div class='productOrinizePrice'>Giá bìa: <span>" + data[i].OrinizePrice + " VNĐ</span></div>"
                    bestSell += "<div class='productPrice'>Giá bán: <span>" + data[i].Price + " VNĐ</span></div>";
                    bestSell += "<a href='#' class='productBuy'>Đặt hàng</a><div class='productActions'><a href='#'>Chia sẻ</a> <a href='#'>Mời xem</a> <a href='#'>Lưu lại</a></div></div>";

                    $("#Products").append(bestSell);
                }

                groupCaroufredselItems(".Products", 6);
                $(".Products").carouFredSel({
                    direction: "left",
                    pagination: "#BestSellPages",
                    auto: false,
                    'scroll': { 'item': 1, 'duration': 1500, 'fx': 'scroll' }
                });

            }
        }
    });

    var catalogs = $(".Catalogs");
    $(".CategoryItem").click(function () {
        if ($(this).next(".Catalogs").css("display") != "none") return;
        catalogs.slideUp("medium");
        $(this).next(".Catalogs").slideDown("medium");
    });

    $(".marqueSlide").carouFredSel({
        direction: "up",
        auto: true,
        item: 1,
        duration: 5000
    });

    $("#ProductsRecommendItems").carouFredSel({
        direction: "left",
        pagination: "#ProductsRecommendPages",
        auto: false,
        'scroll': { 'item': 2, 'duration': 1500, 'fx': 'scroll' }
    });
    $("#ProductsNewItems").carouFredSel({
        direction: "left",
        pagination: "#ProductsNewPages",
        auto: false,
        'scroll': { 'item': 4, 'duration': 1500, 'fx': 'scroll' }
    });
});


function groupCaroufredselItems(element, itemcount) {
    var container = $(element);
    var arr = container.children();
    var i = 0;
    var html = "";
    var temphtml = "";
    var total = container.children().size();
    for (var j = 0; j < total; j++) {
        if (i++ == itemcount - 1) {
            i = 0;
            html += '<div>' + temphtml + '<div class="clear"/></div>';
            temphtml = "";
        }
        temphtml += arr[j].outerHTML;
    }
    container.html(html);
}